package com.study.mike.common.utils;


import com.study.mike.model.dto.TreeNode;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author KangHui
 * @Date 2022/8/8 14:52
 */
public class TreeUtil {
    public static List<? extends TreeNode> buildTree(List<? extends TreeNode> nodes) {
        Map<Long, List<TreeNode>> sub = nodes.stream().filter(node -> node.getParentId() != 0).
                collect(Collectors.groupingBy(TreeNode::getParentId));
        nodes.forEach(node -> node.setChildren(sub.get(node.getId())));
        return nodes.stream().filter(node -> node.getParentId() == 0).collect(Collectors.toList());
    }

    /**
     * 构造用户资源树
     * @param nodes
     * @return
//     */
//    public static List<UserResourceDTO> buildUserResourceTree(List<UserResourceDTO> nodes) {
//        Map<Long, List<UserResourceDTO>> sub = nodes.stream().filter(node -> node.getParentId() != 0).
//                collect(Collectors.groupingBy(UserResourceDTO::getParentId));
//        nodes.forEach(node -> node.setChildren(sub.get(node.getId())));
//        return nodes.stream().filter(node -> node.getParentId() == 0).collect(Collectors.toList());
//    }


}
